﻿Imports NPOI.SS.UserModel
Imports Sy.Data

Public Class CommonInvoke
    Inherits SingletonObject(Of CommonInvoke)
#Region "CommonReturn"
    Private Class GetResult
        Inherits JsonObject(Of GetResult)
        Public Sub New() : End Sub
        Public Sub New(state As Boolean, data As String)
            Me.state = state
            Me.Data = data
        End Sub
        Public state As Boolean
        Public Data As String
    End Class
    Public Function GetReslut(state As Boolean, Output As String) As String
        Return New GetResult(state, Output).ToJson()
    End Function
#End Region
#Region "Expand Keywords"
    Public Function GetUrlByID(Keyword As String, Channel As Integer)
        If Channel = 0 Then
            Return "https://www.google.com/complete/search?client=hp&q=" & Keyword & "&xhr=t"
#Region "Example"
            '["LOL",[["lol",0],["lol符文",0],["lol手機版下載",0],["lolskin",0],["lol手游",0],["lol下載",0],["lol加速器",0],["lol自走棋手機版",0],["lol官网",0],["lol助手",0]],{"k":1,"q":"J9O9ENuPeB6LPVEAdupfzD6uPL8"}]
#End Region
        ElseIf Channel = 1 Then
            Return "https://search.yahoo.com/sugg/gossip/gossip-us-ura/?output=sd1&command=" & Keyword
#Region "Example"
            '{"q":"lol","l":{"gprid":"1Te1hjjnRL25OUWr_LkB1A"},"r":[{"k":"lol","m":6},{"k":"lol dolls","m":6},{"k":"lolo jones","m":6},{"k":"lola consuelos","m":6},{"k":"lol skin","m":6},{"k":"lol coloring pages","m":6},{"k":"lol download","m":6},{"k":"lol surprise dolls","m":6},{"k":"lol surprise","m":6},{"k":"lolita davidovich","m":6}]}
#End Region
        ElseIf Channel = 2 Then
            Return "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" & Keyword & "&json=1"
#Region "Example"
            'window.baidu.sug({"q":"lol","p":false,"bs":"","g":[{"q":"lol\u5b98\u7f51","t":"n","st":{"q":"lol\u5b98\u7f51","new":0}},{"q":"LOL","t":"n","st":{"q":"LOL","new":0}},{"q":"lol\u624b\u6e38","t":"n","st":{"q":"lol\u624b\u6e38","new":0}},{"q":"lol\u4ec0\u4e48\u610f\u601d","t":"n","st":{"q":"lol\u4ec0\u4e48\u610f\u601d","new":0}},{"q":"lol\u4e91\u9876\u4e4b\u5f08","t":"n","st":{"q":"lol\u4e91\u9876\u4e4b\u5f08","new":0}},{"q":"lol\u6bd4\u8d5b","t":"n","st":{"q":"lol\u6bd4\u8d5b","new":0}},{"q":"lol\u6625\u5b63\u8d5b","t":"n","st":{"q":"lol\u6625\u5b63\u8d5b","new":0}},{"q":"Lol","t":"n","st":{"q":"Lol","new":0}},{"q":"lol\u534a\u4ef7\u5427","t":"n","st":{"q":"lol\u534a\u4ef7\u5427","new":0}},{"q":"lol\u514b\u9686\u6a21\u5f0f\u5f00\u542f\u65f6\u95f42020","t":"n","st":{"q":"lol\u514b\u9686\u6a21\u5f0f\u5f00\u542f\u65f6\u95f42020","new":0}}],"s":["lol\u5b98\u7f51","LOL","lol\u624b\u6e38","lol\u4ec0\u4e48\u610f\u601d","lol\u4e91\u9876\u4e4b\u5f08","lol\u6bd4\u8d5b","lol\u6625\u5b63\u8d5b","Lol","lol\u534a\u4ef7\u5427","lol\u514b\u9686\u6a21\u5f0f\u5f00\u542f\u65f6\u95f42020"]})
#End Region
        End If
        Return String.Empty
    End Function
    Public Function GetKeywordsByID(Keyword As String, Channel As Integer, html As Sy.String) As List(Of String)
        Dim keywords As New List(Of String)
        If Channel = 0 Then
            Dim Spliter = $"[""{Keyword}"",["
            If html.StartsWith(Spliter) Then html = html.TrimStart(Spliter)
            Spliter = "],["
            If html.Contains(Spliter) Then
                Dim PageLst = html.Split(Spliter)
                Dim SpliterS = """", SpliterE = """"
                For Each Page In PageLst
                    If Page.CanMid(SpliterS, SpliterE) Then
                        Dim k = Page.Mid(SpliterS, SpliterE)
                        keywords.Add(k)
                    End If
                Next
            End If
        ElseIf Channel = 1 Then
            Dim YahooItem As YahooItem = YahooItem.DeserializeSafety(html)
            If YahooItem.IsNotNull() AndAlso YahooItem.r.IsNotNull() AndAlso YahooItem.r.Count > 0 Then
                For Each item In YahooItem.r
                    Dim k = item.k
                    keywords.Add(k)
                Next
            End If
        ElseIf Channel = 2 Then
            Dim StartStr = "(", EndStr = ")", EndStr1 = ";"
            If html.Contains(StartStr) Then html = html.After(StartStr)
            If html.EndsWith(EndStr1) Then html = html.TrimEnd(EndStr1)
            If html.EndsWith(EndStr) Then html = html.TrimEnd(EndStr)
            html = Sy.Text.Encode.RemoveEscape_Java(html)
            Dim BaiduItem As BaiduItem = BaiduItem.DeserializeSafety(html)
            If BaiduItem.IsNotNull() AndAlso BaiduItem.g.IsNotNull() AndAlso BaiduItem.g.Count > 0 Then
                For Each item In BaiduItem.g
                    Dim k = item.q
                    keywords.Add(k)
                Next
            End If
        End If
        Return keywords
    End Function
    Public Class YahooItem
        Inherits JsonObject(Of YahooItem)
        Public Property r As List(Of rItem)
        Public Class rItem
            Public Property k As String
        End Class
    End Class
    Public Class BaiduItem
        Inherits JsonObject(Of BaiduItem)
        Public Property g As List(Of gItem)
        Public Class gItem
            Public Property q As String
        End Class
    End Class
#End Region
#Region "SendEmail"

#End Region
#Region "Frm"
    Public Function FullData(Sheet As ISheet, StartRow As Integer, Data As List(Of List(Of String)), Log As LogSub) As Integer
        Dim RowNum = Data.Count
        Dim ExportNum = 0
        For i = 0 To RowNum - 1
            Dim Row = Sheet.CreateRow(StartRow + i)
            Dim RowData = Data(i)
            Dim ColNum = RowData.Count
            Dim CanExport = True
            '检测当前数据中是否有超过最大字符的数据,存在则跳过导出
            RowData.ForEach(Sub(item)
                                If item.IsNotNull() AndAlso item.Length > 32767 Then
                                    CanExport = False
                                    Log($"跳过导出[{RowData(1)}]数据,无法存入Excel,存在有超过最大字符限制(32767)的数据[{item.Substring(0, 50)}]")
                                    Return
                                End If
                            End Sub)
            '可导出
            If CanExport Then
                ExportNum += 1
                For ColIndex = 0 To ColNum - 1
                    Dim Cell = Row.CreateCell(ColIndex)
                    Dim ExportData = RowData(ColIndex)
                    Cell.SetCellValue(ExportData)
                Next
            End If
        Next
        Return ExportNum
    End Function
#End Region
End Class
